/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Lab;
import ulti.HibernateUtil;

/**
 *
 * @author phong
 */
public class LabDAO {
    //<editor-fold defaultstate="collapsed" desc="1. Lấy danh sách các Lab">

    public static List<Lab> getLabs() {
        List<Lab> Labs = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Lab";
            Query query = session.createQuery(hql);
            Labs = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return Labs;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2. Lấy thông tin Lab thông qua id">
    public static Lab getLabByID(String id) {
        Lab Lab = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Lab = (Lab) session.get(Lab.class, id);
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return Lab;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3. Lấy danh sách các Lab free tai 1 thoi diem">
    public static List<Lab> getFreeLabByTime(Date useDate, String timeTableID) {
        List<Lab> Labs = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
             String hql = hql = "from Lab l where l.id  not in (select s.lab.id from SignLab s"
                + " where s.useDate = :useDate and s.timeTable.id = :timeTableID"
                + " and (s.status = 'Approve' or s.status ='Done' ))";
            Query query = session.createQuery(hql);
            query.setDate("useDate", useDate);
            query.setString("timeTableID", timeTableID);
            Labs = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        }
        return Labs;
    }
    //</editor-fold>
}
